<template>
  <!-- App -->
  <f7-page color-theme="blues" id="loginmain">
    <div id="login" ref="login" class="form-success">
      <span @click="goback" class="login-back">返回</span>
      <div class="logoBox">
        <img src="@/assets/image/logo.jpg" alt="" />
      </div>
      <div class="container">
        <transition>
          <h1 @click="change">{{ islogin ? "登 录" : "注 册" }}</h1>
        </transition>
        <transition name="slide-fade">
          <form class="form" v-show="loginBox">
            <transition name="slide-fade">
              <input
                type="text"
                placeholder="请输入昵称"
                v-show="!islogin"
                v-model.trim="loginForm.nickName"
                @blur="nickNameIpt"
                ref="nickName"
              />
            </transition>

            <input
              type="text"
              placeholder="请输入手机号码"
              v-model.trim="loginForm.userName"
              @blur="userNameIpt"
              ref="userName"
            />
            <input
              type="password"
              placeholder="请输入密码"
              v-model.trim="loginForm.password"
              @blur="passwordIpt"
              ref="password"
            />
            <transition name="slide-fade">
              <input
                type="text"
                placeholder="请输入邮箱"
                v-show="!islogin"
                v-model.trim="loginForm.email"
                ref="email"
                @blur="emailIpt"
              />
            </transition>

            <transition name="slide-fade">
              <span class="idCode">
                <input
                  v-model.trim="loginForm.code"
                  type="text"
                  auto-complete="off"
                  placeholder="验证码"
                  ref="code"
                  @keyup.enter="submit"
                />

                <img
                  :src="identifyCodeUrl"
                  id="kaptchaImage"
                  @click="getCode"
                />
              </span>
            </transition>

            <transition name="slide-fade">
              <div class="aBox" v-show="islogin">
                <a href="#" @click.stop="forgetPass"> 忘记密码 </a>
              </div>
            </transition>

            <transition name="slide-fade" mode="out-in">
              <button id="login-button1" @click="submit">
                {{ islogin ? "登 录" : "注 册" }}
              </button>
            </transition>

            <transition name="slide-fade" mode="out-in">
              <button id="login-button2" @click="change">
                {{ islogin ? " 注 册" : " 登 录" }}
              </button>
            </transition>
          </form>
        </transition>
      </div>
      <ul class="bg-bubbles">
        <li></li>
        <li></li>
        <li></li>
        <li></li>
        <li></li>
        <li></li>
        <li></li>
        <li></li>
        <li></li>
        <li></li>
      </ul>
    </div>
  </f7-page>
</template>

<script>
import store from "@/store";
import { login, getCodeImg, registered, forgetPwd } from "@/api/login";
import Cookies from "js-cookie";
import { encrypt } from "@/utils/jsencrypt";
import {
  Validate_nickName,
  Validate_userName,
  Validate_email,
  Validate_password,
} from "@/utils/fromValidate";
export default {
  data() {
    return {
      loginBox: false,
      islogin: true,
      savePwd: false,
      loginForm: {
        nickName: "",
        userName: "",
        password: "",
        email: "",
        rememberMe: false,
        code: "",
        uuid: "",
        userType: "01",
      },
      identifyCodeUrl: "",
    };
  },
  mounted() {
    // 初始化动画
    setTimeout(() => {
      this.loginBox = true;
      this.islogin = true;
      this.$refs.login.setAttribute("class", "form-login");
    }, 500);
    this.getCode();
  },
  befordestroyed() {
    this.loginBox = false;
    this.islogin = true;
    this.savePwd = false;
    this.$refs.login.setAttribute("class", "form-success");
    for (let key in this.loginForm) {
      if (key == "userType") {
        this.loginForm[key] = "01";
      } else if (key == "rememberMe") {
        this.loginForm[key] = false;
      } else {
        this.loginForm[key] = "";
      }
    }
  },
  methods: {
    goback() {
      this.$f7router.back();
    },
    submit(event) {
      this.$refs.code.blur();
      event.preventDefault();
      if (this.islogin) {
        this.logoin();
      } else {
        this.register();
      }
    },
    logoin() {
      if (this.loginForm.userName == "" || this.loginForm.password == "") {
        this.$f7.dialog.alert("账号或密码为空", "提示");
        return;
      }
      // 提交时登录状态
      this.$refs.login.setAttribute("class", "form-success");
      this.loginBox = !this.loginBox;

      if (this.loginForm.rememberMe) {
        Cookies.set("userName", this.loginForm.userName, { expires: 30 });
        Cookies.set("password", encrypt(this.loginForm.password), {
          expires: 30,
        });
        Cookies.set("rememberMe", this.loginForm.rememberMe, {
          expires: 30,
        });
      } else {
        Cookies.remove("userName");
        Cookies.remove("password");
        Cookies.remove("rememberMe");
      }

      login(this.loginForm).then((res) => {
        if (res.code == 200) {
          this.$store.dispatch("Login", res).then(() => {
            this.loading = false;
            this.$refs.login.setAttribute("class", "form-login");
            this.loginBox = !this.loginBox;
            this.goback();
          });
        } else {
          this.loading = false;
          this.getCode();
          this.$f7.dialog.alert(res.msg, "提示");
          this.$refs.login.setAttribute("class", "form-login");
          this.loginBox = !this.loginBox;
        }
      });
    },
    uploadImgHead(imgType, md5, data) {
      this.$f7.preloader.show();
      uploadHead(imgType, md5, data).then((data) => {
        this.$f7.preloader.hide();
        if (data.status == 200) {
          let headUrl = data.data;
          this.user.avatar = headUrl;
          this.avatarImg = IMG_URL + "/" + headUrl;
          this.$f7.popup.close("#registerUpload");
        } else {
          this.$f7.dialog.alert(data.msg, "提示");
        }
      });
    },
    show() {
      this.$refs.login.setAttribute("class", "form-login");
      this.loginBox = !this.loginBox;
    },
    change(event) {
      event.preventDefault();
      this.islogin = !this.islogin;
    },
    forgetPass() {
      // this.$f7.dialog.password("请输入账号", "提示", function (userName) {
      //   forgetPwd({ userName: userName }).then((data) => {

      //   });
      // });
      this.$f7.dialog.alert("待开放", "提示");
    },
    passwordIpt(event) {
      if (Validate_password(event.target.value)) {
        this.$refs.password.style = "none";
      } else {
        this.$refs.password.style = "border:1px solid red";
        event.target.value = "";
        this.$refs.password.placeholder = "数字 + 字母组合6位以上";
      }
    },
    register() {
      if (this.loginForm.nickName.length <= 0)
        return this.$f7.dialog.alert("昵称格式错误", "提示");
      if (this.loginForm.userName.length <= 0)
        return this.$f7.dialog.alert("账号格式错误", "提示");
      if (this.loginForm.password.length <= 0)
        return this.$f7.dialog.alert("密码格式错误", "提示");
      if (this.loginForm.email.length <= 0)
        return this.$f7.dialog.alert("邮箱格式错误", "提示");

      this.$f7.preloader.show();

      registered(this.loginForm).then((data) => {
        this.$f7.preloader.hide();
        let _this = this;
        if (data.code == 200) {
          this.$f7.dialog.alert(data.msg, "提示", function () {
            _this.clearFrom();
            _this.islogin = true;
          });
        } else {
          this.$f7.dialog.alert(data.msg, "提示");
        }
      });
    },
    nickNameIpt(event) {
      if (Validate_nickName(event.target.value)) {
        this.$refs.nickName.style = "none";
      } else {
        this.$refs.nickName.style = "border:1px solid red";
        event.target.value = "";
        this.$refs.nickName.placeholder = "2-6位中文字符";
      }
    },
    userNameIpt(event) {
      if (Validate_userName(event.target.value)) {
        this.$refs.userName.style = "none";
      } else {
        this.$refs.userName.style = "border:1px solid red";
        event.target.value = "";
        this.$refs.userName.placeholder = "手机号码有误";
      }
    },
    emailIpt(event) {
      if (Validate_email(event.target.value)) {
        this.$refs.email.style = "none";
      } else {
        this.$refs.email.style = "border:1px solid red";
        this.$refs.email.placeholder = "请输入正确的邮箱!";
      }
    },
    getCode() {
      getCodeImg().then((res) => {
        this.identifyCodeUrl = "data:image/gif;base64," + res.img;
        this.loginForm.uuid = res.uuid;
      });
    },
    clearFrom() {
      for (let key in this.loginForm) {
        this.loginForm[key] = "";
      }
    },
  },
};
</script>

<style scoped lang="scss">
.upload {
  width: 5%;
  height: 55px;
}

#login {
  background: #50a3a2;
  background: -webkit-linear-gradient(top left, #50a3a2 0%, #53e3a6 100%);
  background: linear-gradient(to bottom right, #50a3a2 0%, #53e3a6 100%);
  opacity: 0.8;
  position: absolute;
  width: 100%;
  height: 100%;
  overflow: hidden;
}

#login.form-success .container h1 {
  -webkit-transform: translateY(85px);
  -ms-transform: translateY(85px);
  transform: translateY(85px);
}

#login.form-login .container h1 {
  -webkit-transform: translateY();
  -ms-transform: translateY();
  transform: translateY();
}

.form-login {
  top: 0;
}

.logoBox {
  position: relative;
  width: 100%;
  height: 20%;
  margin-bottom: 5%;
  p {
    width: 10%;
    margin-right: 5%;
    float: right;
    font-size: 18px;
    // padding: 10px 20px 0 0px;
    z-index: 2;
    position: relative;
  }
  img {
    position: absolute;
    left: 50%;
    top: 50%;
    transform: translate(-50%, -50%);
    background: transparent;
    width: 5rem;
    height: 5rem;
    border-radius: 2.5rem;
  }
}

.container {
  max-width: 600px;
  margin: -5% auto 0;
  padding: 30px 0;
  height: 400px;
  text-align: center;
  h1 {
    font-size: 40px;
    -webkit-transition-duration: 1s;
    transition-duration: 1s;
    -webkit-transition-timing-function: ease-in-put;
    transition-timing-function: ease-in-put;
    font-weight: 200;
    cursor: pointer;
    width: 250px;
    margin: 0 auto;
    position: relative;
    z-index: 2;
  }
}

form {
  padding: 20px 0;
  position: relative;
  z-index: 2;
  div,
  input {
    -webkit-appearance: none;
    -moz-appearance: none;
    appearance: none;
    outline: 0;
    border: 1px solid rgba(255, 255, 255, 0.4);
    background-color: rgba(255, 255, 255, 0.2);
    width: 250px;
    border-radius: 3px;
    padding: 10px 15px;
    margin: 0 auto 10px auto;
    display: block;
    text-align: center;
    font-size: 18px;
    color: white;
    -webkit-transition-duration: 0.25s;
    transition-duration: 0.25s;
    font-weight: 300;
  }

  input:hover {
    background-color: rgba(255, 255, 255, 0.4);
  }

  input:focus {
    background-color: white;
    width: 300px;
    color: #53e3a6;
    -moz-box-shadow: 0 0 0 3px #d85c89;
    -webkit-box-shadow: 0 0 0 3px #d85c89;
    box-shadow: 0 0 0 3px #d85c89;
  }

  button {
    display: block;
    -webkit-appearance: none;
    -moz-appearance: none;
    appearance: none;
    outline: 0;
    background-color: white;
    border: 0;
    padding: 10px 15px;
    color: #53e3a6;
    margin: 0 auto 10px auto;
    border-radius: 3px;
    width: 250px;
    cursor: pointer;
    font-size: 18px;
    -webkit-transition-duration: 0.25s;
    transition-duration: 0.25s;
  }

  img {
    display: block;
    -webkit-appearance: none;
    -moz-appearance: none;
    appearance: none;
    outline: 0;
    background-color: white;
    border: 0;
    padding: 8px 8px;
    margin: 0 auto 10px auto;
    border-radius: 3px;
    width: 50px;
    height: 3rem;
    display: block;
    cursor: pointer;
    font-size: 18px;
    -webkit-transition-duration: 0.25s;
    transition-duration: 0.25s;
  }

  button:hover {
    background-color: #f5f7f9;
  }

  input:nth-last-child(3) {
    width: 3rem;
    // background: url("../assets/img/logo.png");
  }
}

.idCode {
  display: inline-block;

  input {
    float: left;
    width: 130px;
    margin: 0 20px 10px auto;
  }

  input:focus {
    width: 130px;
  }

  img {
    float: left;
    width: 100px;
    height: 43px;
  }
}

.aBox {
  width: 100%;
  border: 0;
  background-color: transparent;
  margin-left: 95px;
  line-height: 1rem;
  text-decoration: none;
  color: blue;
  font-weight: 500;
  font-size: 16px;
}

.bg-bubbles {
  position: absolute;
  top: 0;
  left: 0;
  width: 100%;
  height: 100%;
  z-index: 1;
  li {
    position: absolute;
    list-style: none;
    display: block;
    width: 40px;
    height: 40px;
    background-color: rgba(255, 255, 255, 0.15);
    bottom: -160px;
    -webkit-animation: swuare 25s infinite;
    animation: square 25s infinite;
    -webkit-transition-timing-function: linear;
    transition-timing-function: linear;
  }

  li:nth-child(1) {
    left: 10%;
  }

  li:nth-child(2) {
    left: 20%;
    width: 80px;
    height: 80px;
    -webkit-animation-delay: 2s;
    animation-delay: 2s;
    -webkit-animation-duration: 17s;
    animation-duration: 17s;
  }

  li:nth-child(3) {
    left: 25%;
    -webkit-animation-delay: 4s;
    animation-delay: 4s;
  }

  li:nth-child(4) {
    left: 40%;
    width: 60px;
    height: 60px;
    -webkit-animation-duration: 22s;
    animation-duration: 22s;
    background-color: rgba(255, 255, 255, 0.25);
  }

  li:nth-child(5) {
    left: 70%;
  }

  li:nth-child(6) {
    left: 80%;
    width: 120px;
    height: 120px;
    -webkit-animation-delay: 3s;
    animation-delay: 3s;
    background-color: rgba(255, 255, 255, 0.2);
  }

  li:nth-child(7) {
    left: 32%;
    width: 160px;
    height: 160px;
    -webkit-animation-delay: 7s;
    animation-delay: 7s;
  }

  li:nth-child(8) {
    left: 55%;
    width: 20px;
    height: 20px;
    -webkit-animation-delay: 15s;
    animation-delay: 15s;
    -webkit-animation-duration: 40s;
    animation-duration: 40s;
  }

  li:nth-child(9) {
    left: 25%;
    width: 10px;
    height: 10px;
    -webkit-animation-delay: 2s;
    animation-delay: 2s;
    -webkit-animation-duration: 40s;
    animation-duration: 40s;
    background-color: rgba(255, 255, 255, 0.3);
  }

  li:nth-child(10) {
    left: 90%;
    width: 160px;
    height: 160px;
    -webkit-animation-delay: 11s;
    animation-delay: 11s;
  }
}

.slide-fade-enter-active {
  transition: all 0.5s ease;
}
.slide-fade-leave-active {
  transition: all 0.8s cubic-bezier(1, 0.5, 0.8, 1);
}
.slide-fade-enter, .slide-fade-leave-to
/* .slide-fade-leave-active for below version 2.1.8 */ {
  transform: translateX(10px);
  opacity: 0;
}

@-webkit-keyframes square {
  0% {
    -webkit-transform: translateY(0);
    transform: translateY(0);
  }

  100% {
    -webkit-transform: translateY(-700px) rotate(600deg);
    transform: translateY(-700px) rotate(600deg);
  }
}

@keyframes square {
  0% {
    -webkit-transform: translateY(0);
    transform: translateY(0);
  }

  100% {
    -webkit-transform: translateY(-700px) rotate(600deg);
    transform: translateY(-700px) rotate(600deg);
  }
}

//------- vue动画
.fadeIn {
  -webkit-animation: 1s fadeIn cubic-bezier(0.015, 0.695, 0.34, 1.365) backwards;
  -moz-animation: 1s fadeIn cubic-bezier(0.015, 0.695, 0.34, 1.365) backwards;
  animation: 1s fadeIn cubic-bezier(0.015, 0.695, 0.34, 1.365) backwards;
  -webkit-animation-delay: 1.5s;
  -moz-animation-delay: 1.5s;
  animation-delay: 1.5s;
}

//--------跳动小球
.load-state {
  -moz-transition-duration: 0.2s;
  -o-transition-duration: 0.2s;
  -webkit-transition-duration: 0.2s;
  transition-duration: 0.2s;
}
.load-state {
  position: absolute;
  top: -50px;
  left: 0;
  right: 0;
  height: 100%;
  text-align: center;
  line-height: 50px;
  pointer-events: none;
}
.load-state .ball {
  height: 10px;
  width: 10px;
  margin-right: 10px;
  background: #fff;
  display: inline-block;
  -moz-border-radius: 1000px;
  -webkit-border-radius: 1000px;
  border-radius: 1000px;
  -webkit-animation: ballBounce 0.3s alternate infinite;
  -moz-animation: ballBounce 0.3s alternate infinite;
  animation: ballBounce 0.3s alternate infinite;
  -webkit-animation-timing-function: cubic-bezier(0.2, 0.4, 0.3, 1);
  -moz-animation-timing-function: cubic-bezier(0.2, 0.4, 0.3, 1);
  animation-timing-function: cubic-bezier(0.2, 0.4, 0.3, 1);
  -moz-transition-duration: 0.3s;
  -o-transition-duration: 0.3s;
  -webkit-transition-duration: 0.3s;
  transition-duration: 0.3s;
}
.load-state .ball:nth-child(2n) {
  -webkit-animation-delay: 0.05s;
  -moz-animation-delay: 0.05s;
  animation-delay: 0.05s;
}
.load-state .ball:nth-child(3n) {
  -webkit-animation-delay: 0.1s;
  -moz-animation-delay: 0.1s;
  animation-delay: 0.1s;
}
.load-state .ball:last-child {
  margin-right: 0;
}
.load-state {
  -moz-transform: translateY(50px);
  -ms-transform: translateY(50px);
  -webkit-transform: translateY(50px);
  transform: translateY(50px);
}

.login-back {
  position: absolute;
  font-size: 16px;
  color: rgb(31, 29, 29);
  margin: 10px;
  z-index: 9999;
}
</style>

